package de.baumann.browser.present

import de.baumann.browser.OdinApplication
import de.baumann.browser.R
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.AccountInfo
import de.baumann.browser.api.net.vo.LoginInfo
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IMineView
import de.baumann.browser.model.UserModel
import de.baumann.browser.utils.*

class MinePresenter : BasePresenter<IMineView>() {

    private val userModel = UserModel()

    fun getProfitAndHash() {
        this.userModel.odinData
                .subscribe(object : OdinObserver<AccountInfo>(view, false) {
                    override fun onSuccess(data: AccountInfo) {
                        if (view != null) {
                            view.setTotalProfit(data.profit)
                            if (data.contribute == null) {
                                view.setTotalHash("0")
                            } else {
                                view.setTotalHash(data.contribute)
                            }
                        }
                    }
                })
    }

    fun getUserBasicInfo() {
        val loginInfo = getUser()
        if (loginInfo == null && view != null) {
            view.setHeadhUrl("")
            view.setLoginName(OdinApplication.getInstance().getString(R.string.login_now))
            return
        }
        if (loginInfo != null && view != null) {
            view.setHeadhUrl(loginInfo.headUrl)
            val name = if (loginInfo.user_name.isEmpty()) {
                loginInfo.loginName
            } else {
                loginInfo.user_name
            }

            view.setLoginName(name)
            view.setPoolName(loginInfo.poolname)
            if (loginInfo.licence != null) {
                view.setLicenceStr(loginInfo.licence)
            }
        }
    }

    fun login() {
        userModel.login(getMoneyAddress(), "")
                .subscribe(object : OdinObserver<LoginInfo>(view) {
                    override fun onSuccess(loginInfo: LoginInfo) {
                        saveUser(loginInfo)
                        UnicornUtil.setUserInfo(loginInfo)
                        saveInviteCode(loginInfo.inviteCode)
                        saveInviteCodeStatus(loginInfo.inviteId)
                        saveFormAddress(loginInfo.fromAddress)
                        saveLogin(true)
                    }
                })
    }
//    fun getUserRole() {
//        CardModel().getUserRole()
//                .subscribe(object : OdinObserver<Role>(view,false) {
//                    override fun onSuccess(data: Role?) {
//                        if (view!=null && data!=null){
//                            view.setUserRole(data.type)
//                        }
//                    }
//                })
//    }
}